package com.top.up.controller;

import com.top.up.annotation.LogInfo;
import com.top.up.base.RequestBean;
import com.top.up.base.ResultBean;
import com.top.up.biz.WXOauthBiz;
import com.top.up.core.dto.oauth.AccessTokenDTO;
import com.top.up.core.dto.oauth.WxOauthDTO;
import com.top.up.core.dto.oauth.WxOauthReqDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/auth")
@Api(tags = "授权接口")
public class OauthController {

    @Autowired
    private WXOauthBiz wxOauthBiz;

    @PostMapping("/wx/token")
    @ApiOperation(value = "小程序全局唯一后台接口调用凭据")
    @LogInfo(description = "小程序全局唯一后台接口调用凭据")
    public ResultBean<AccessTokenDTO> authAccessToken(@RequestBody RequestBean<String> req){
        return new ResultBean.Builder<AccessTokenDTO>()
                .fromRequest(req)
                .data(wxOauthBiz.token(req.getChannelCode()))
                .successBuild();
    }

    @PostMapping("/2/session")
    @ApiOperation(value = "登录凭证校验")
    @LogInfo(description = "登录凭证校验")
    public ResultBean<WxOauthDTO> auth2Session(@RequestBody RequestBean<WxOauthReqDTO> req){
        return new ResultBean.Builder<WxOauthDTO>()
                .fromRequest(req)
                .data(wxOauthBiz.auth2Session(req.getChannelCode(), req.getData()))
                .successBuild();
    }

    @PostMapping("/userInfo")
    @ApiOperation(value = "授权获取用户信息")
    @LogInfo(description = "授权获取用户信息")
    public ResultBean<WxOauthDTO> getUserInfo(@RequestBody RequestBean<WxOauthReqDTO> req){
        return new ResultBean.Builder<WxOauthDTO>()
                .fromRequest(req)
                .data(wxOauthBiz.authUserInfo(req.getChannelCode(), req.getData()))
                .successBuild();
    }

}
